<?php
// $Id: dish.class.php 117 2011-12-28 08:21:23Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Zone_Controller extends AdminController{

	function init(){}

	function onIndex(){
		$pid   = array_get_int($_GET, 'pid');
		$data  = $this->getZoneList($pid);

		$this->setVar('pid',  $pid);
		$this->setVar('data', $data);
		$this->render('admin/zone_list');
	}

	function onCreate(){
		$pid = array_get_int($_GET, 'pid');

		$this->setVar('pid',    $pid);
		$this->setVar('action', $this->action);
		$this->render('admin/zone_edit');
	}

	function onModify(){
		$zone_id = array_get_int($_GET, 'zone_id');
		$data    = $this->getZoneInfo($zone_id);

		$this->setVar('pid',    $data['parent_id']);
		$this->setVar('data',   $data);
		$this->setVar('action', $this->action);
		$this->render('admin/zone_edit');
	}

	function onProcess(){

		switch(array_get_string($_POST, 'action')){
			case 'create':
				$this->insert($_POST);
				$pid = array_get_int($_POST, 'parent_id');
				$message = '区域信息添加成功！';
				$operate = array(
					'继续添加信息' => Base::adminUrl($this->controller, 'create', array('pid' => $pid)),
					'返回列表页'   => Base::adminUrl($this->controller, 'index', array('pid' => $pid)),
				);
				break;
			case 'modify':
				$this->update($_POST);
				$pid = array_get_int($_POST, 'parent_id');
				$message = '区域信息更新成功！';
				$operate = array(
					'返回列表页' => Base::AdminUrl($this->controller, 'index', array('pid' => $pid)),
				);
				break;
			case 'delete':
				//$this->module->delStore(array_get_int($_POST, 'res_id'));
				$message = '区域信息删除成功！';
				break;
		}

		$this->renderDialog($message, $operate);
	}

	function onBatch(){
		$sorts = array();
		if(isset($_POST['select']) && is_array($_POST['select'])){
			foreach($_POST['select'] as $zone_id){
				$list[$zone_id] = array_get($_POST['list'], $zone_id);
				$list[$zone_id]['zone_id'] = $zone_id;
			}
		}

		$action  = 'index';
		switch(array_get_string($_POST, 'action')){
			case 'delete':
				$this->module->deleteArray(array_keys($sorts));
				$message = '区域信息删除成功！';
				break;
			case 'sort':
				$this->sortArray($list);
				$message = '区域信息排序成功！';
				break;
		}
		$operate = array('返回列表页' => Base::adminUrl($this->controller, $action, array('pid' => array_get_int($_POST, 'pid'))));

		$this->renderDialog($message, $operate);
	}

	private function checkData($post){
	}

	private function filteData($post, $edit = false){

		$ename  = array_get_string($post, 'ename', null);
		$letter = array_get_string($post, 'letter', '');
		$ztypes = array_get($post, 'zonetype');
		$zone_type = 0;
		foreach($ztypes as $zt){
			$zone_type |= $zt;
		}

		$data = array(
			'name'	  => array_get_string($post, 'name'),
			'ename'	  => empty($ename) ? null : strtolower($ename),
			'letter'  => substr($letter, 0, 1),
			'zone_type' => $zone_type,
			'longitude' => array_get_double($post, 'longitude'),
			'latitude'  => array_get_double($post, 'latitude'),
			'radius'    => array_get_int($post, 'radius'),
			'display' 	=> array_get_bool($post, 'display', 1, 0),
		);

		if(!$edit){
			$data['parent_id'] = array_get_int($post, 'parent_id');
			$data['city_id']   = $this->cityid;
			$data['zone_id']   = array_get_int($post, 'zone_id');
		}

		return $data;
	}

	private function getZoneList($pid){
		return DB::getAll("select zone_id, parent_id, name, ename, letter, ordnum, zone_type, display
						   from `t_zone` where `city_id` = ? and parent_id = ? order by `ordnum`",
					array($this->cityid, intval($pid)));
	}

	private function getZoneInfo($zone_id){
		return $this->getZoneBy('zone_id', intval($zone_id));
	}

	private function getZoneBy($key, $var){
		return DB::getOne("select * from `t_zone` where `city_id` = ? and `{$key}` = ?",
					array($this->cityid, $var));
	}

	private function getZoneOnly($ename, $zone_id = 0){
		return DB::getOne("select * from `t_zone` where `city_id` = ? and `ename` = ? and `zone_id` <> ?",
					array($this->cityid, $ename, intval($zone_id)));
	}

	private function update($post){
		$zone_id = array_get_int($post, 'zone_id');
		$ename   = array_get_string($post, 'ename');
		if(!$this->getZoneOnly($ename, $zone_id)){
			return DB::update('t_zone', $this->filteData($post, true), array('city_id' => $this->cityid, 'zone_id' => $zone_id));
		}else{
			return false;
		}
	}

	private function insert($post){
		$ename   = array_get_string($post, 'ename');
		if(!$this->getZoneBy('ename', $ename)){
			return DB::insert('t_zone', $this->filteData($post, false));
		}else{
			return false;
		}
	}

	private function sortArray($zones){
		$stmt = DB::prepare("update `t_zone` set `ordnum` = ?  where `city_id` = ? and `zone_id` = ?");
		foreach($zones as $item){
			extract($item);
			$stmt->execute(array(intval($ordnum), $this->cityid, intval($zone_id)));
		}
		return true;
	}

	private function deleteArray($zones){
		$stmt = DB::prepare("delete `t_zone` where `city_id` = ? and `zone_id` = ?");
		foreach($zones as $item){
			extract($item);
			$stmt->execute(array($this->cityid, intval($zone_id)));
		}
		return true;
	}

	function __destruct(){
		//print_r($this->db);
		//print_r($_POST);
	}
}
?>